/*
 * client.cpp
 *
 *  Created on:  Nov 16, 2008
 *      Author:  stephen durfey
 *      pid:     P006560229
 *      purpose: a simple client program. connects to the specified server with the specified port number. once
 *               a connection is established, the client will finger the appropriate username, as specified by
 *               the user as a command line argument. it can accept n number of users to finger.
 */

#include "clientServer.h"
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>
#include <cstring>


int connectToHost (int , char * , int );

using namespace std;

int main(int argc, char* argv[])
{
	if(argc > 3)
	{
		int socketNum;
		int result;
		int readBytes;
		int portNum = atoi(argv[2]);
		char fingerResult[512];

		for(int user = 3; user < argc ; user++)
		{
			socketNum = socket(AF_INET, SOCK_STREAM, 0);

			result = connectToHost(socketNum, argv[1], portNum);

			if(result == -1)
			{
				cout << "connection failed\n";
				exit(1);
			}

			write(socketNum, argv[user], strlen(argv[user]));
			write(socketNum, "\n", 1);

			readBytes = read(socketNum, fingerResult, 512);

			for(int index = 0; index < readBytes; index++)
			{
				cout << fingerResult[index];
			}
		}

		close(socketNum);
	}
	else
		cout << "Program usage: ./client <hostname> 79 user1 user2 user3 .... usern\n";

	return 0;
}

/*

~/cs327/clientServer$ ./client voyager.cs.bgsu.edu 79 gzimmer
hostname is  voyager.cs.bgsu.edu
hello

Login       Name               TTY         Idle    When    Where
gzimmer  Guy Zimmerman         pts/5        <Oct 21 09:13> zimmerman.cs.bg

~/cs327/clientServer$ ./client voyager.cs.bgsu.edu 79 gzimmer rama
hostname is  voyager.cs.bgsu.edu
hello

Login       Name               TTY         Idle    When    Where
gzimmer  Guy Zimmerman         pts/5        <Oct 21 09:13> zimmerman.cs.bg
hostname is  voyager.cs.bgsu.edu
hello

Login       Name               TTY         Idle    When    Where
rama     Ray Kresman           ftp1047      <Sep 17 15:11> allegro.cs.bgsu

~/cs327/clientServer$ ./client voyager.cs.bgsu.edu 79 gzimmer rama chao
hostname is  voyager.cs.bgsu.edu
hello

Login       Name               TTY         Idle    When    Where
gzimmer  Guy Zimmerman         pts/5        <Oct 21 09:13> zimmerman.cs.bg
hostname is  voyager.cs.bgsu.edu
hello

Login       Name               TTY         Idle    When    Where
rama     Ray Kresman           ftp1047      <Sep 17 15:11> allegro.cs.bgsu
hostname is  voyager.cs.bgsu.edu
hello

Login       Name               TTY         Idle    When    Where
jchao    Joe Chao                           <Oct  9 16:10> dhcp-64-36.cs.b

 */






